//
//  HZMBaseWebView.m
//  糖糖大魔王
//
//  Created by 韩震明 on 15/10/27.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "HZMBaseWebView.h"


@interface HZMBaseWebView ()<UIWebViewDelegate>

@property(nonatomic, retain) UIWebView *webView;

@end

@implementation HZMBaseWebView
{
    UIActivityIndicatorView *acView;
}

- (void)dealloc
{
    [_WebViewStrUrl release];
    [_titleForWeb release];
    [_webView release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createNavigation];
    
    
    [self createWebView];
}



#pragma Navigation Setting
- (void)createNavigation
{
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    
    self.navigationItem.title = self.titleForWeb;
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(-20, 0, 40, 40);
    [leftButton setImage:[UIImage imageNamed:@"nav_back@2x.png"] forState:UIControlStateNormal];
    [self.view addSubview:leftButton];
    [leftButton addTarget:self action:@selector(leftBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton] autorelease];
    self.navigationController.navigationBar.translucent = NO;
    
}

#pragma mark Navigationbar ButtonAction
- (void)leftBarButtonClick:(UIBarButtonItem *)leftBarButton
{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark WebView Setting
- (void)createWebView
{
    NSString *title = @"好物详情";
    NSString *buy = @"宝物详情";
    if (self.navigationItem.title == title) {
        self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, -50, self.view.frame.size.width, self.view.frame.size.height)];
    } else if ([self.navigationItem.title isEqualToString:buy]) {
//        [self.navigationController setNavigationBarHidden:NO animated:YES];
        if (self.navigationController.hidesBarsOnSwipe) {
            self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        } else {
        self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
        }
    } else {
        self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    }
    
    [self.view addSubview:self.webView];
    [self.webView release];
    self.webView.delegate  = self;
    self.webView.scalesPageToFit = YES;
    // 风火轮
    acView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 32.0f, 32.0f)];
    acView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    
    acView.center = self.view.center;
    [self.view addSubview:acView];
    
    [self createData];
}

- (void)createData
{
    NSURL *url = [NSURL URLWithString:self.WebViewStrUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

#pragma mark WebView Delegate Method
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [acView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [acView stopAnimating];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    // 忽略异步加载返回 -999
    if ([error code] == NSURLErrorCancelled) return;
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"网络错误" message:@"连接失败" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self.navigationController popViewControllerAnimated:YES];
        
    }];
    UIAlertAction *reload = [UIAlertAction actionWithTitle:@"重新加载" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self createData];
        
    }];
    [alert addAction:cancel];
    [alert addAction:reload];
    [self presentViewController:alert animated:YES completion:nil];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
